1523C - Compression and Expansion - CodeForces Solution


brute force data structures greedy implementation trees *1600

Please click on ads to support us..

C++ Code:

//      BISMILLAH-IR-RAHMAN-IR-RAHIM
 
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<float, float> pff;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
#define el << '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds ;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
#define pb push_back
#define PI 3.14159265358979323
#define file() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
#define fastIO	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
ll bin_expo(ll base, ll pow, ll m) {
        if (pow == 0) return 1;
        ll ret = bin_expo(base, pow / 2, m) % m;
        if (pow & 1) return (ret % m * ret % m * base % m) % m;
        else return (ret % m * ret % m) % m;
}
ll mod_add(ll a, ll b, ll m) { return ((a % m) + (b % m)) % m; }
ll mod_sub(ll a, ll b, ll m) { return ((a % m) - (b % m)) % m; }
ll mod_mul(ll a, ll b, ll m) { return ((a % m) * (b % m)) % m; }
ll mod_div(ll a, ll b, ll m) { return ((a % m) * bin_expo(b, m - 2, m)) % m; }
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
int dx[] = {0, 0, +1, -1, +1, -1, -1, +1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};
const ll infLL = 4e18 + 123;
const ll inf = 1e17 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 1e9 + 9;
const int mx = 2e5 + 123;
const double eps = 1e-7;
 
 
// forever-striver 
 
 
void solution() { 
 
        ll n ; 
        cin >> n  ; 
        stack<ll>stk ; 
        for(ll i=0;i<n;i++) {
                ll x ; 
                cin >> x ; 
                stack<ll>tmp; 
                tmp = stk ; 
                vl v ; 
                if(x==1) {
                        stk.push(1) ; 
                        tmp.push(1) ; 
                        while(!tmp.empty()) {
                                v.push_back(tmp.top()) ; 
                                tmp.pop() ; 
                        } 
                }
                else {
                        while(!tmp.empty() and tmp.top()!=(x-1)) {
                                //v.push_back(tmp.top()) ; 
                                tmp.pop() ; 
                        }
                        v.push_back(x) ; 
                        if(!tmp.empty()) {
                                tmp.pop() ; 
                                while(!tmp.empty()) {
                                        v.push_back(tmp.top()) ; 
                                        tmp.pop() ; 
                                }
                        }
                }
                reverse(v.begin(),v.end()) ; 
                for(int j=0;j<v.size();j++) {
                        if(j<v.size()-1) {
                                cout << v[j] << "." ; 
                        }
                        else cout << v[j] el ; 
                }
                reverse(v.begin(),v.end()) ;
                stack<ll>newstk ; 
                while(v.size()) {
                        newstk.push(v.back()) ; 
                        v.pop_back() ; 
                }
                stk = newstk ; 
        }
 
}
 
 
int main() {
        fastIO;
        // file() ;
        ll _ = 1; cin >> _;
        while (_--) solution();
        return 0 ;
}


Comments

Submit
0 Comments
More Questions

468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions